
/**
 * @file window.c
 * A/52 Kaiser-Bessel Derived Window
 */

#include "common.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "window.h"


double a52_window[512] = {0};

void a52_window_apply(double *dest_samples, double *src_samples)
{
    int i;
    for (i = 0; i < 512; i++)
        dest_samples[i] = src_samples[i] * a52_window[i];
}

/**
 * Generate a Kaiser-Bessel Derived Window.
 * @param ctx           The context
 * @param alpha         Determines window shape
 * @param out_window    Array to fill with window values
 * @param n             Full window size
 * @param iter          Number of iterations to use in BesselI0
 */
static void kbd_window_init(double alpha, double *window, int n, int iter)
{
    int i, j, n2;
    double a, x, bessel, sum;

    n2 = n >> 1;
    a = alpha * M_PI / n2;
    a = a*a;
    sum = 0.0;
    for (i = 0; i < n2; i++) {
        x = i * (n2 - i) * a;
        bessel = 1.0;
        for (j = iter; j > 0; j--)
            bessel = (bessel * x / (j*j)) + 1.0;
        sum += bessel;
        window[i] = sum;
    }
    sum += 1.0;
    for (i = 0; i < n2; i++) {
        window[i] = sqrt(window[i] / sum);
        window[n-1-i] = window[i];
    }
}

void a52_window_init(void)
{
    kbd_window_init(5.0, a52_window, 512, 50);
}
